	<?php
	/**
	 * @package    Joomla.Tutorials
	 * @subpackage Components
	 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1
	 * @license    GNU/GPL
	 */
	 
	// No direct access
	 
	defined( '_JEXEC' ) or die( 'Restricted access' );
	 
	jimport('joomla.application.component.controller');
	 
	/**
	 * Jobs World Component Controller
	 *
	 * @package    Joomla.Tutorials
	 * @subpackage Components
	 */
	class JobsController extends JController
	{
		/**
		 * Method to display the view
		 *
		 * @access    public
		 */
		function display()
		{
			parent::display();
		}
		function edituserinfo()
		{
			// Check for request forgeries.
			JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
	
			
			// Initialise variables.
			$app	= JFactory::getApplication();
			
			// Get the user data.
			$requestData = JRequest::getVar('jform', array(), 'post', 'array');
	
			// Validate the posted data.
			/*
				$form	= $model->getForm();
				if (!$form) {
					JError::raiseError(500, $model->getError());
					return false;
				}
			*/
			//$data	= $model->validate($form, $requestData);
			
			$user = JFactory::getUser();
			$db =& JFactory::getDBO();
			$query = 'SELECT * from #__users WHERE username = "'.$user->username.'"';
			$db->setQuery( $query );
			$db->query();
			$row = $db->loadObject();	
			//print_r($row);		
			if(isset($row)) {	
				//if();
				
		
			
				$data['firma_navn']=JRequest::getVar('firma_navn', '', 'post', '');
				$data['adresse']=JRequest::getVar('adresse', '', 'post', '');
				$data['post']=JRequest::getVar('post', '', 'post', '');
				$data['by_']=JRequest::getVar('by_', '', 'post', '');
				$data['cvr']=JRequest::getVar('cvr', '', 'post', '');
				$data['telefone']=JRequest::getVar('telefone', '', 'post', '');
				
				//$data['by_']=JRequest::getVar('by_', '', 'post', '');
				
				$data['name']=JRequest::getVar('name', '', 'post', '');
				$data['lastname']=JRequest::getVar('lastname', '', 'post', '');
				$data['username']=JRequest::getVar('username', '', 'post', '');
				//$data['email']=JRequest::getVar('email', '', 'post', '');
				$data['oldpassword']=JRequest::getVar('oldpassword', '', 'post', '');
				$data['password']=JRequest::getVar('password', '', 'post', '');
				$data['amount_money']=0;
				
				
				//$salt		= JUserHelper::genRandomPassword(32);
				//$salt ='';
				
				$parts	= explode(':', $user->password);
				$crypt	= $parts[0];
				$salt	= @$parts[1];
				$testcrypt = JUserHelper::getCryptedPassword($data['oldpassword'], $salt);
					
				if ($crypt == $testcrypt) {
					$db =& JFactory::getDBO();
					
					$salt		= JUserHelper::genRandomPassword(32);
					$crypted	= JUserHelper::getCryptedPassword($data['password'], $salt);
					$password	= $crypted.':'.$salt;
								
					$query = "UPDATE #__users SET firma_navn=".$db->quote($data['firma_navn']).",";
					$query .= "adresse=".$db->quote($data['adresse']).",";
					$query .= "post=".$db->quote($data['post']).",";
					$query .= "by_=".$db->quote($data['by_']).",";
					$query .= "cvr=".$db->quote($data['cvr']).",";
					$query .= "telefone=".$db->quote($data['telefone']).",";
					$query .= "name=".$db->quote($data['name']).",";
					if(strlen($data['password']) >0) {
						$query .= "password=".$db->quote($password).",";
					}
					$query .= "lastname=".$db->quote($data['lastname']);
					//$query = "adresse=".$data['adresse'];
					$query .= " WHERE id = ".$user->id;
					$db->setQuery( $query );
					$db->query();
					//echo $query;
					//exit();
					
					$items = getArticles(54);
					$slug = $items[0]->id.':'.$items[0]->alias;
					$catslug =$items[0]->catid ? $items[0]->catid .':'.$items[0]->category_alias : $items[0]->catid;
					require_once JPATH_SITE.'/components/com_content/helpers/route.php';
					$readmore = JRoute::_(ContentHelperRoute::getArticleRoute($slug, $catslug));
					$this->setRedirect($readmore);
					
			
				} else {
					$link = 'index.php?option=com_jobs&view=myprofile';
					$this->setRedirect($link, 'Indtast venligst nuværende adgangskode');
					
				}
		
				//$crypted	= JUserHelper::getCryptedPassword($data['password'], $salt);
				//echo $crypted;
				//$password	= $crypted.':'.$salt;
				
				
				//print_r($data);
				//exit();
			}
		}
		function changeJobStatus()
		{
			$work_id	= JRequest::getVar('work_id','', 'get');	
			$select_id	= JRequest::getVar('select_id','', 'get');	
			$db =& JFactory::getDBO();
			if(isset($work_id)&&isset($select_id)) {
				//if($select_id != 45) {
					$query = 'SELECT * from #__uworks WHERE id = '.$work_id;
					$db->setQuery( $query );
					$db->query();
					$row = $db->loadObject();	
					$jobid = $row->jobid;
					
					$items_job = JModel::getInstance( 'myjobs', 'JobsModel');
					$oldstatus = $items_job->getCategoriesName($row->catid);
					$newstatus = $items_job->getCategoriesName($select_id);
					
					$rowtitlejob = $items_job->getDetailJob($jobid);
									
					$query = 'UPDATE #__uworks SET catid = "'.$select_id.'" WHERE id = '.$work_id;
					$db->setQuery( $query );
					if($db->query()) {
						$this->sentEmailUpdateStatus($jobid,$rowtitlejob->title,$oldstatus->title,$newstatus->title);
					}
				//}
			}			 
		}
		public function sentEmailUpdateStatus($id,$title,$oldstatus,$newstatus)
		{
			$user = JFactory::getUser();
				
			$html = $user->firma_navn." har foretaget følgende statusændring:";
			
			$html.= "<br/>Opgave:".$id."/".$title;
			$html.= "<br/>Ændret fra:".$oldstatus;
			$html.= "<br/>Til:".$newstatus;
			
			$html.= "<br/><br/>Med venlig hilsen,
					<br/><a href='http://www.3malertilbud.dk'><b>3malertilbud.dk</b></a
					<br/><a href='mailto:info@3malertilbud.dk'>info@3malertilbud.dk</a>";
			$html.= "<br/><img src='".JURI::root()."/templates/3dmaler/img/email_logo_footer.jpg'></img>";	
				
			$app		= JFactory::getApplication();	
			$mailfrom	= $app->getCfg('mailfrom');
			$fromname	= $app->getCfg('fromname');
			$sitename	= $app->getCfg('sitename');
			$name		= '';
			$email		= $mailfrom;
			
			$subject	= '3malertilbud.dk statusændring af '.$user->firma_navn;
			$body		= $html;
			
			// Prepare email body
			//JURI::base();
			$body	= stripslashes($body);
			$mail = JFactory::getMailer();
			//echo $mailfrom;
			
			//echo $html;
			//exit();
			$mail->sendMail($mailfrom, $fromname, $email, $subject, $body, $mailfrom);	
			
		} 
		
		public function sentPurchasedATaskMail($job_id,$firma_navn,$user_email)
		{
			
			//$html= "<br/>Hej:".$firma_navn;
			$html= "ID : ".$job_id;
			$html.= "<br/>Firmanavn : ".$firma_navn;
			$html.= "<br/>E-mail-adresse : ".$user_email;
			//$html.= "Firmanavn :".$name;
			
			$html.= "<br/><br/>Med venlig hilsen,
					<br/><a href='http://www.3malertilbud.dk'><b>3malertilbud.dk</b></a
					<br/><a href='mailto:info@3malertilbud.dk'>info@3malertilbud.dk</a>";
			$html.= "<br/><img src='".JURI::root()."/templates/3dmaler/img/email_logo_footer.jpg'></img>";	
				
			$app		= JFactory::getApplication();	
			$mailfrom	= $app->getCfg('mailfrom');
			$fromname	= $app->getCfg('fromname');
			$sitename	= $app->getCfg('sitename');
			$name		= '';
			$receivedemail		= 'status@malertilbud.dk';
			//$receivedemail		= 'le.dung@mwc.vn';
			
			$subject	= 'Købt en opgave ID : '.$job_id;
			$body		= $html;
			
			// Prepare email body
			//JURI::base();
			$body	= stripslashes($body);
			$mail = JFactory::getMailer();
			//echo $mailfrom;
			
			//echo $html;
			//exit();
			$mail->sendMail($mailfrom, $fromname, $receivedemail, $subject, $body, $mailfrom);	
			
		} 
		function addtomyjob() {
						
			$jobid = JRequest::getVar( 'id','','post' );
			
			$items_job = JModel::getInstance( 'myjobs', 'JobsModel');
			$job = $items_job->getDetailJob($jobid);
			$user = JFactory::getUser();
			$status = $user->guest;		
			$date= gmdate('Y-m-d H:i:s');
			//echo $jobid; 
			$point=$job->point;
			
			if($status != 1){
				$db =& JFactory::getDBO();
				$query = "SELECT amount_money FROM #__users WHERE ID=".$user->id;
				$db->setQuery( $query );
				$row = $db->loadObject();				
				$amount_money = $row->amount_money - $point;
				//print_r($user);
				//print_r($job);
				
				$query = "SELECT count(*) as total FROM #__uworks WHERE state != -2 AND userid=".$user->id." AND jobid=".$job->id;
				$db->setQuery( $query );
				$rowcountuser = $db->loadObject();
				
				$query = "SELECT count(*) as total FROM #__uworks WHERE state != -2 AND jobid=".$job->id;
				$db->setQuery( $query );
				$row = $db->loadObject();
     			//echo '--'.$row->total;
				 if($row->total <=2 && ($amount_money >= 0) && $rowcountuser->total==0)
				 {	
				 	
				 
						$query = "INSERT INTO #__uworks(catid,title,created,point,userid,jobid) values(41,'ID.".$job->id." (".$date.")', '".$date."','".$point."','".$user->id."','".$job->id."')";
						 $db->setQuery( $query );
						if($db->query())
						{
							//  Arbejde begyndt 42;
							//$query = "UPDATE #__jobs SET workstatus = '42' WHERE ID = ".$job->id;					
							//$db->setQuery( $query );							
							//$db->query();
							$query = "UPDATE #__users SET amount_money = '".$amount_money."' WHERE ID = ".$user->id;											
							$db->setQuery( $query );							
							$db->query();
							
							@$this->sentPurchasedATaskMail($job->id,$user->firma_navn,$user->email);
							
							//echo $user->amount_money;	
						}
				 }
			}
			
			?>
            <script type="text/javascript">
				$(document).ready(function($) {	
					$('#alert').html('Opgaven er nu købt og kan findes<br/> under “Mine opgaver”');
					$('#backoverlay').show();
					$('#show_popup').show();
				});
            </script>
            <?php
			parent::display();
			
			//$this->assignRef('items_job',		$items_job);
			
			
		}
		
		function fileUpload($max, $upload_dir='tmp', $file_type, $msg,$file){
		//Retrieve file details from uploaded file, sent from upload form	
		// Retorna: Array ( [name] => mod_simpleupload_1.2.1.zip [type] => application/zip 
		// [tmp_name] => /tmp/phpo3VG9F [error] => 0 [size] => 4463 ) 
		$filename = "";
		
		if(isset($file)){ 
				//Clean up filename to get rid of strange characters like spaces etc
				
				$filename = JFile::makeSafe(time().'_'.$file['name']);
				
				//if($file['size'] > $max) $msg = JText::_('ONLY_FILES_UNDER').' '.$max;
				//Set up the source and destination of the file
		 
				$src = $file['tmp_name'];
				//$dest = JPATH_BASE.DS.'images'.DS.'jobfiles' .  DS . $filename;
				$dest = JPATH_BASE.DS.'tmp'.  DS . $filename;
				
				
				//First check if the file has the right extension, we need jpg only
				if ( in_array($file['type'],$file_type)) { 			
				//echo $dest;			
					if ( JFile::upload($src, $dest) ) {	 
					   //Redirect to a page of your choice
					//$msg = JText::_('FILE_SAVE_AS').' '.$dest;
				   } else {
					  //Redirect and throw an error message
					$msg = JText::_('ERROR_IN_UPLOAD');
				   }
				} else {
				   //Redirect and notify user file is not right extension
					$msg = JText::_('FILE_TYPE_INVALID');
				}
			//exit();
			}
			return array($filename,$msg);
		}
		
		function job_step_first()
		{
			$session =& JFactory::getSession();						
			
			//$max = 1048576;
			$max = 5242880;		 
			$file_type = array("image/png","image/gif","image/jpeg","image/jpg","image/pjpeg");
			$file01 = '';
			$error1 = '';		
			$file02 = '';
			$error2 = '';		
			$file03 = '';
			$error3 = '';	
			$uploadimage1 = JRequest::getVar("file01", null, 'files'); 
			$uploadimage2 = JRequest::getVar("file02", null, 'files'); 
			$uploadimage3 = JRequest::getVar("file03", null, 'files'); 
			
			
			if($uploadimage1['error']==0) {
				list($file01,$error1) = $this->fileUpload($max,'',$file_type,'',$uploadimage1);
			}
			if($uploadimage2['error']==0) {
				list($file02,$error2) = $this->fileUpload($max,'',$file_type,'',$uploadimage2);
			}
			if($uploadimage3['error']==0) {
				list($file03,$error3) = $this->fileUpload($max,'',$file_type,'',$uploadimage3);
			}
			//save session
			$job_title =  JRequest::getVar( 'job_title','','post' );
			$session->set('job_title', $job_title);
			$job_description =  JRequest::getVar( 'job_description','','post' );
			$session->set('job_description',$job_description);
			$job_soon =  JRequest::getVar( 'job_soon','','post' );		
			if($job_soon=='on') $job_soon = 1; else $job_soon = 0;		
			$session->set('job_soon', $job_soon);
			$datepicker =  JRequest::getVar( 'datepicker','','post' );
			$session->set('datepicker',$datepicker);
			$catid = JRequest::getVar( 'catid','','post' );
			$session->set('catid',$catid);
			$session->set('file01', $file01);
			$session->set('file02', $file02);
			$session->set('file03', $file03);
			$link = 'index.php?option=com_jobs&view=register';
			$this->setRedirect($link, '');
		}
		function job_step_second()
		{
			
			$session =& JFactory::getSession();	
			
			$job_company =  JRequest::getVar( 'job_company','','post' );
			$usertype =  JRequest::getVar( 'usertype','','post' );
			if($usertype==1) {
				$job_company =  JRequest::getVar( 'job_company','','post' );
			} else {
				$job_company =  '';
			}
			//if($usertype =='on') { $job_usertype=1;} else { $job_usertype=0;}
			
			$job_firstname =  JRequest::getVar( 'job_firstname','','post' );
			$job_lastname =  JRequest::getVar( 'job_lastname','','post' );
			$job_address =  JRequest::getVar( 'job_address','','post' );
			$job_post =  JRequest::getVar( 'job_post','','post' );
			$job_city =  JRequest::getVar( 'job_city','','post' );
			$job_email =  JRequest::getVar( 'job_email','','post' );
			if(trim($job_email)=="") {
				$link = 'index.php?option=com_jobs&view=jobs';
				$this->setRedirect($link, '');
			}
			$job_tel =  JRequest::getVar( 'job_tel','','post' );
			$job_workataddress =  JRequest::getVar( 'job_workataddress','','post' );
			if($job_workataddress==1) {
				$job_address1 =  '';
				$job_post1 =  '';
				$job_city1 = '';
			} else {
			//if($job_workataddress =='on') { $job_workataddress=1;} else { $job_workataddress=0;}
				$job_address1 =  JRequest::getVar( 'job_address1','','post' );
				$job_post1 =  JRequest::getVar( 'job_post1','','post' );
				$job_city1 =  JRequest::getVar( 'job_city1','','post' );
			}
			
			$job_title = $session->get('job_title');
			$job_description = $session->get('job_description');
			$job_catid = $session->get('catid');
			$job_soon = $session->get('job_soon');
			$datepicker = $session->get('datepicker');
			$file01 = $session->get('file01');
			$file02 = $session->get('file02');
			$file03 = $session->get('file03');
			$point=0;
			$session->set('email_post_jobs', $job_email);	
			//mktime(0,0,0,date(),$day,$year)
			$adeadline = explode('-',$datepicker);
			//$datepicker= $adeadline[2].'-'.$adeadline[1].'-'.$adeadline[0];
			
			$datepicker = gmdate('Y-m-d H:i:s',strtotime($datepicker));
			$date= gmdate('Y-m-d H:i:s');
			//gmdate('Y-m-d H:i:s',);
			$query = "SELECT max(ordering) as 'maxorder' FROM #__jobs";
			//global $db;
			$db = JFactory::getDbo();
			$db->setQuery( $query );
			$row=$db->loadObject();
			$ordering = 0;
			if($row) {
				$ordering = $row->maxorder+1;
			}
			$poldpath = JPATH_BASE.DS.'tmp'.  DS ;
			
			$fileurl1 = "";
			$fileurl2 = "";
			$fileurl3 = "";
			if(strlen($file01) > 0) {
				$fileurl1 = 'images/jobfiles/'.$file01;
			}
			if(strlen($file02) > 0) {
				$fileurl2 = 'images/jobfiles/'.$file02;
			}
			if(strlen($file03) > 0) {
				$fileurl3 = 'images/jobfiles/'.$file03;
			}
			$query = "INSERT INTO `#__jobs` (`catid`, `title`, `description`, `created`,`ordering`,`state`,`fileurl1`,`fileurl2`,`fileurl3`,`soon`,`deadline`,`point`,`usertype`,`company`,`firstname`,`lastname`,`address`,`postcode`,`city`,`email`,`telephone`,`workataddress`,`address1`,`postcode1`,`city1`) VALUES (".$db->quote($job_catid).", ".$db->quote($job_title).", ".$db->quote($job_description).", ".$db->quote($date).", ".$db->quote($ordering).",0, ".$db->quote($fileurl1).", ".$db->quote($fileurl2).", ".$db->quote($fileurl3).", ".$db->quote($job_soon).", ".$db->quote($datepicker).", ".$db->quote($point).", ".$db->quote($usertype).", ".$db->quote($job_company).", ".$db->quote($job_firstname).", ".$db->quote($job_lastname).", ".$db->quote($job_address).", ".$db->quote($job_post).", ".$db->quote($job_city).", ".$db->quote($job_email).", ".$db->quote($job_tel).", ".$db->quote($job_workataddress).", ".$db->quote($job_address1).", ".$db->quote($job_post1).", ".$db->quote($job_city1).")";
			
			//echo $query;
			
			if(is_file($poldpath.$file01))
			{			
				$pnewpath = JPATH_BASE.DS.'images'. DS.'jobfiles'.DS ;
				copy($poldpath.$file01,$pnewpath.$file01);
				unlink($poldpath.$file01);
			}
			if(is_file($poldpath.$file02))
			{			
				$pnewpath = JPATH_BASE.DS.'images'. DS.'jobfiles'.DS ;
				copy($poldpath.$file02,$pnewpath.$file02);
				unlink($poldpath.$file02);
			}
			if(is_file($poldpath.$file03))
			{			
				$pnewpath = JPATH_BASE.DS.'images'. DS.'jobfiles'.DS ;
				copy($poldpath.$file03,$pnewpath.$file03);
				unlink($poldpath.$file03);
			}
			
				$db->setQuery( $query );
				$db->query();
				$last_job_id = $db->insertid();
			
			//$last_job_id = 111111;
			
			if(strlen($file01) > 0) {
				$lfile01 = '<a href="'.JURI::root().'images/jobfiles/'.$file01.'">'.$file01.'</a>';
			} else {
				$lfile01 = '';
			}
			if(strlen($file02) > 0) {
				$lfile02 = '<a href="'.JURI::root().'images/jobfiles/'.$file02.'">'.$file02.'</a>';
			} else {
				$lfile02 = '';
			}
			if(strlen($file03) > 0) {
				$lfile03 = '<a href="'.JURI::root().'images/jobfiles/'.$file03.'">'.$file03.'</a>';
			} else {
				$lfile03 = '';
			}
			
			//echo $query;
		
			
			/*
				A user who has published a project, can not login or see the project they have registered. But they will receive an email about all the informations they have enter
				Also the create ID-number be show in the mail . In the mail as we dicuss early, they can choose to delete the project and the project will automatic be unpublished in the backend (NOT DELETE in the backend)
			*/
			
			//$siteURL	= JURI::root() . 'index.php?option=com_messages&view=message&message_id='.$table->message_id;
			//$sitename	= JFactory::getApplication()->getCfg('sitename');
			//$subject	= '';
			//$msg		= '';
			//JFactory::getMailer()->sendMail($fromUser->email, $fromUser->name, $toUser->email, $subject, $msg);
			$marler = base64_encode('malertilbud'.$last_job_id);
			$link_deactive = JURI::root().'index.php?option=com_jobs&view=deactive&marler='.$marler;
			if($usertype==1) {
				$html = "Kære ".$job_company;
			} else {
				$html = "Kære ".$job_firstname;
				//." ".$job_lastname;
			}
			
			$html .="<br/><br/>Din opgave er nu oprettet i vores system og afventer godkendelse. Det kan tage op til 24 timer, før opgaven er aktiv og synlig for malerfirmaerne.";

			$html .="<br/><br/><b>Her kan du se dine indtastede oplysninger:</b><br/>";
			$html .="<hr style='width:285px;text-align:left;clear:both;'><br/>";
			$html .= '<table>';
				$html .= '<tr>';
					$html .= '<td>';
					$html .= 'ID:';
					$html .= '</td>';
					$html .= '<td>';
					$html .= $last_job_id;
					$html .= '</td>';
				$html .= '</tr>';	
				
				$html .= '<tr>';
					$html .= '<td>';
					$html .= 'Overskrift på din opgave:';
					$html .= '</td>';
					$html .= '<td>';
					$html .= $job_title;
					$html .= '</td>';
				$html .= '</tr>';
				$html .= '<tr>';
					$html .= '<td>';
					$html .= 'Beskriv din maleropgave:';
					$html .= '</td>';
					$html .= '<td>';
					$html .= $job_description;
					$html .= '</td>';
				$html .= '</tr>';
				$html .= '<tr>';
					$html .= '<td>';
					$html .= 'Arbejdet ønskes påbegyndt hurtigst muligt?';
					$html .= '</td>';
					$html .= '<td>';
					if($job_soon==1) {
						$html .= 'Ja';
					} else {
						$html .= 'Nej';
					}
					$html .= '</td>';
				$html .= '</tr>';
				$html .= '<tr>';
					$html .= '<td>';
					$html .= 'Arbejdet ønskes afsluttet:';
					$html .= '</td>';
					$html .= '<td>';
					$html .=  JHtml::_('date', $datepicker , JText::_('DATE_FORMAT_LC5'));
					$html .= '</td>';
				$html .= '</tr>';
				$html .= '<tr>';
					$html .= '<td>';
					$html .= 'Jeg mener, at opgaven ca. vil koste:';
					$html .= '</td>';
					$html .= '<td>';
					
					$query = "SELECT * FROM #__categories WHERE id=". $job_catid;					
					$db->setQuery( $query );
					$pris_cat=$db->loadObject();
					
					$html .= $pris_cat->title;
					
					$html .= '</td>';
				$html .= '</tr>';
				if($usertype==1) {
				$html .= '<tr>';
					$html .= '<td>';
					$html .= 'Firma navn:';
					$html .= '</td>';
					$html .= '<td>';
					$html .= $job_company;
					$html .= '</td>';
				$html .= '</tr>';
				}
				$html .= '<tr>';
					$html .= '<td>';
					$html .= 'Fornavn:';
					$html .= '</td>';
					$html .= '<td>';
					$html .= $job_firstname;
					$html .= '</td>';
				$html .= '</tr>';
				$html .= '<tr>';
					$html .= '<td>';
					$html .= 'Efternavn:';
					$html .= '</td>';
					$html .= '<td>';
					$html .= $job_lastname;
					$html .= '</td>';
				$html .= '</tr>';
				$html .= '<tr>';
					$html .= '<td>';
					$html .= 'Adresse:';
					$html .= '</td>';
					$html .= '<td>';
					$html .= $job_address;
					$html .= '</td>';
				$html .= '</tr>';
				$html .= '<tr>';
					$html .= '<td>';
					$html .= 'Postnr:';
					$html .= '</td>';
					$html .= '<td>';
					$html .= $job_post;
					$html .= '</td>';
				$html .= '</tr>';
				$html .= '<tr>';
					$html .= '<td>';
					$html .= 'By:';
					$html .= '</td>';
					$html .= '<td>';
					$html .= $job_city;
					$html .= '</td>';
				$html .= '</tr>';
				$html .= '<tr>';
					$html .= '<td>';
					$html .= 'E-mail:';
					$html .= '</td>';
					$html .= '<td>';
					$html .= $job_email;
					$html .= '</td>';
				$html .= '</tr>';
				$html .= '<tr>';
					$html .= '<td>';
					$html .= 'Telefonnummer:';
					$html .= '</td>';
					$html .= '<td>';
					$html .= $job_tel;
					$html .= '</td>';
				$html .= '</tr>';
				$html .= '<tr>';
					$html .= '<td>';
					$html .= 'Skal arbejdet udføres på ovenstående adresse?';
					$html .= '</td>';
					$html .= '<td>';
					if($job_workataddress==0) {
						$html .= 'Nej';
					} else {
						$html .= 'Ja';
					}							
					$html .= '</td>';
				$html .= '</tr>';
				if($job_workataddress==0) {
				$html .= '<tr>';
					$html .= '<td>';
					$html .= 'Arbejdet skal udføres på denne adresse:';
					$html .= '</td>';
					$html .= '<td>';
					$html .= $job_address1;
					$html .= '</td>';
				$html .= '</tr>';	
				$html .= '<tr>';
					$html .= '<td>';
					$html .= 'Postnummer:';
					$html .= '</td>';
					$html .= '<td>';
					$html .= $job_post1;
					$html .= '</td>';
				$html .= '</tr>';	
				$html .= '<tr>';
					$html .= '<td>';
					$html .= 'By:';
					$html .= '</td>';
					$html .= '<td>';
					$html .= $job_city1;
					$html .= '</td>';
				$html .= '</tr>';	
				}
				$html .= '<tr>';
					$html .= '<td>';
					$html .= 'Uploadede billeder:';
					$html .= '</td>';
					$html .= '<td>';
					if(strlen($lfile01)==0 && strlen($lfile02)==0 && strlen($lfile03)==0)
					{  
						$html .= 'Ingen billeder uploaded';
					} else {
						$html .= $lfile01;
						$html .= strlen($lfile01)==0 ? "" : ",";
						$html .= $lfile02;
						$html .= strlen($lfile02)==0 ? "" : ",";
						$html .= $lfile03;
					}
					$html .= '</td>';
				$html .= '</tr>';
				
			$html .='</table>';		
			$html .="<hr style='width:285px;text-align:left;clear:both;'>";
			$html.="<br/><br/>Hvis du har ændringer eller rettelser til ovenstående oplysninger, bedes du skrive til os på <a href='mailto:info@3malertilbud.dk'>info@3malertilbud.dk</a>.";
				
			$html.='<br/><br/><b>Skulle du ønske at slette din annonce, kan du trykke <a href="'.$link_deactive.'"> her:</a></b>';
			$html.="<br/><br/>Med venlig hilsen,
					<br/><b>3malertilbud.dk</a>
					<br/><a href='mailto:info@3malertilbud.dk'>info@3malertilbud.dk</a>";
			$html.="<br/><img src='".JURI::root()."/templates/3dmaler/img/email_logo_footer.jpg'></img>";		
			
			$app		= JFactory::getApplication();
			
			$mailfrom	= $app->getCfg('mailfrom');
			$fromname	= $app->getCfg('fromname');
			$sitename	= $app->getCfg('sitename');
			
			$name		= '';
			$email		=  $job_email;
			$subject	= 'Din opgave er nu oprettet i vores system og afventer godkendelse.';
			$body		= $html;

			// Prepare email body
			//JURI::base();
			$body	= stripslashes($body);
			$mail = JFactory::getMailer();
			
			//echo $html;
			//exit();
			$session->set('file01', "");
			$session->set('file02', "");
			$session->set('file03', "");
			
			if($last_job_id > 0) {
				//$a_email = array($email,'status@3malertilbud.dk');
				@$mail->sendMail($mailfrom, $fromname, $email, $subject, $body, $mailfrom);	
				@$mail->sendMail($mailfrom, $fromname, 'status@3malertilbud.dk', 'Kopi af '.$subject, $body, $mailfrom);		
				//$mail->sendMail($mailfrom, $fromname, $email, $subject, $body, $mailfrom);										
				//$link = 'index.php?option=com_jobs&popup=1&receivedemail='.$email;		
					
				//$this->setRedirect($link, '');	
				$items = getArticles(55);
				$slug = $items[0]->id.':'.$items[0]->alias;
				$catslug =$items[0]->catid ? $items[0]->catid .':'.$items[0]->category_alias : $items[0]->catid;
				require_once JPATH_SITE.'/components/com_content/helpers/route.php';
				$readmore = JRoute::_(ContentHelperRoute::getArticleRoute($slug, $catslug));
				$this->setRedirect($readmore);		
			} else {
				$link = 'index.php?option=com_jobs&view=jobs';		
						
				$this->setRedirect($link, '');	
			}
					 
			/*
			$mail = JFactory::getMailer();
			$mail->addRecipient($mailfrom);
			$mail->addReplyTo(array($email, $name));
			$mail->setSender(array($mailfrom, $fromname));
			$mail->setSubject($subject);
			$mail->setBody($body);
			$sent = $mail->Send();
			*/
			
			
		}
		
		function save()
		{
			// Check for request forgeries
			JRequest::checkToken() or jexit( 'Invalid Token' );
			$post	= JRequest::get('post');
			
			
			
			$model = $this->getModel('register');
			$listimage = JRequest::getVar("upload_listimage", null, 'files', 'array'); 
			
			if($model->checkFileUpload($listimage))
			{
				if ($model->store($post)) {		
					$msg = JText::_( 'Actor Saved' );
				} else {
					$msg = JText::_( 'Error Saving Actor' );
				}
				$link = 'index.php?option=com_jobs&view=register&Itemid='.$post['Itemid'].'&lang='.$post['lang'];
				$this->setRedirect($link, $msg);
			}
			else {
			
				$msg ='Invalid upload file';
				
				$link = 'index.php?option=com_jobs&view=register&Itemid='.$post['Itemid'].'&lang='.$post['lang'];
				
				$this->setRedirect($link, $msg);
				//echo '222222';
			}
			
		}
	
	 
	}
	?>